package com.cskaoyan.day14;

import java.util.Objects;

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        Star star = new Star(18, "Star");
        Student student = new Student(18, "Student", star);
        Teacher teacher = new Teacher(58, "Teacher", student);
        Teacher cloneTeacher = teacher.clone();
        teacher.stu.s.name = "明星";
        teacher.name = "老师";
        teacher.stu.name = "学生";
        teacher.stu.age = 1;
        System.out.println(teacher);
        System.out.println(cloneTeacher);

    }
}

class Teacher implements Cloneable {
    int age;
    String name;
    Student stu;

    public Teacher(int age, String name, Student stu) {
        this.age = age;
        this.name = name;
        this.stu = stu;
    }

    @Override
    protected Teacher clone() throws CloneNotSupportedException {
        Teacher clone = (Teacher) super.clone();
        Student clone1 = stu.clone();
        clone.stu = clone1;
        return clone;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Teacher teacher = (Teacher) o;
        return age == teacher.age &&
                Objects.equals(name, teacher.name) &&
                Objects.equals(stu, teacher.stu);
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", stu=" + stu +
                '}';
    }
}

class Student implements Cloneable {
    int age;
    String name;
    Star s;

    public Student(int age, String name, Star s) {
        this.age = age;
        this.name = name;
        this.s = s;
    }

    @Override
    protected Student clone() throws CloneNotSupportedException {
        Student studentClone = ((Student) super.clone());
        Star clone = s.clone();
        studentClone.s = clone;
        return studentClone;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name) &&
                Objects.equals(s, student.s);
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", s=" + s +
                '}';
    }
}

class Star implements Cloneable {
    int age;
    String name;

    public Star(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    protected Star clone() throws CloneNotSupportedException {
        return (Star) super.clone();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Star star = (Star) o;
        return age == star.age &&
                Objects.equals(name, star.name);
    }

    @Override
    public String toString() {
        return "Star{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}